package com.jaiai.work.work.api;

import com.jaiai.work.util.RestResultGenerator;
import com.jaiai.work.work.entity.TWork;
import com.jaiai.work.work.service.ITWorkService;
import com.jaiai.work.work.service.impl.TWorkServiceImpl;
import com.jaiai.work.work.vo.WorkSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 工作日志 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2018-04-20
 */
@RestController
@RequestMapping("/api/v1/work/")
public class TWorkController {
    private  static  final Logger log= LoggerFactory.getLogger(TWorkController.class);

    @Autowired
    private ITWorkService workService;


    /**
     * 查询工作报告
     * @param search
     * @return
     */
    @GetMapping("/list")
    public  Object list(@RequestBody WorkSearch search){
        try {
           return RestResultGenerator.genSuccessResult(workService.list(search));
        }catch (Exception e){
           log.error("查询工作报告异常! error:"+e.getMessage());
           return  RestResultGenerator.genErrorResult("error");
        }

    }

    /**
     * 添加工作日志
     * @param work
     * @return
     */
    @PostMapping("/add")
    public  Object addWorkLog(@RequestBody TWork work){
        try {
            workService.insert(work);
            return RestResultGenerator.genSuccessResult("ok");
        }catch (Exception e){
            log.error("添加工作报告异常! error:"+e.getMessage());
            return  RestResultGenerator.genErrorResult("error");
        }
    }

    /**
     * 修改工作日志
     * @param work
     * @return
     */
    @PostMapping("/modify")
    public  Object modifyWorkLog(@RequestBody TWork work){
        try {
            workService.updateById(work);
            return RestResultGenerator.genSuccessResult("ok");
        }catch (Exception e){
            log.error("添加工作报告异常! error:"+e.getMessage());
            return  RestResultGenerator.genErrorResult("error");
        }
    }

    /**
     * 删除工作日志
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public  Object modifyWorkLog(@PathVariable("id") Integer id){
        try {
            workService.deleteById(id);
            return RestResultGenerator.genSuccessResult("ok");
        }catch (Exception e){
            log.error("添加工作报告异常! error:"+e.getMessage());
            return  RestResultGenerator.genErrorResult("error");
        }
    }


}
